const express = require("express");
const Car = require("../model/car");
const router = express.Router();

router.get("/", async (req, res) => {
  const data = await Car.find().sort({
    _id: -1,
  });
  res.json(data);
});

router.delete("/:id", async (req, res) => {
  await Car.findByIdAndRemove(req.params.id);
  res.json({
    code: 1,
    msg: "删除成功",
  });
});

router.post("/", async (req, res) => {
  const data = new Car(req.body);
  await data.save();
  res.json({
    code: 1,
    msg: "添加成功",
  });
});
//通过id查看车辆信息
router.get("/:id", async (req, res) => {
  const data = await Car.findById(req.params.id);
  res.json(data);
});

router.put("/:id", async (req, res) => {
  await Car.findByIdAndUpdate(req.params.id, req.body);
  res.json({
    code: 1,
    msg: "修改成功",
  });
});
module.exports = router;
